﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.Storage;

namespace wBooks.Entity
{
    public static class XmlHelper
    {
        public static Books GetBookDetail(string xmlFile)
        {
            if (xmlFile != null)
            {
                Books book = new Books();
                var xml = XDocument.Load(xmlFile);

                // Query the data and write out a subset of contacts
                var query = from c in xml.Root.Descendants("Name")
                            select c.Value;
                foreach (string name in query)
                {
                    book.Name += name;
                }

                query = from c in xml.Root.Descendants("Author")
                        select c.Value;
                foreach (string name in query)
                {
                    book.Author += name;
                }

                query = from c in xml.Root.Descendants("Images")
                        select c.Value;
                foreach (string name in query)
                {
                    book.Image += name;
                }

                query = from c in xml.Root.Descendants("Category")
                        select c.Value;
                foreach (string name in query)
                {
                    book.CategoryName += name;
                }

                query = from c in xml.Root.Descendants("Book")
                        select c.Attribute("id").Value;
                foreach (string name in query)
                {
                    book.Author += name;
                }

                book.FileName = xmlFile;
                return book;
            }
            else return null;
        }

        // Retrieve the text from the included text file
        public static string LoadBook(string books)
        {
            string s = null;

            var xml = XDocument.Load(books);

            //Query the data and write out a subset of contacts
            var query = from c in xml.Root.Descendants("Chapter")
                        where c.Attribute("name") != null
                        select c.Attribute("name").Value + Environment.NewLine + c.Element("Content").Value + Environment.NewLine;
            //select c.Element("Content").Value;
            foreach (string name in query)
            {
                s += name;
            }

            return s;
        }

        public static async Task<string[]> GetAllAuthor(string folderData)
        {
            string[] author = null;
            // settings
            // same as (ms-appx:///MyFolder/MyFile.txt)
            var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            _Folder = await _Folder.GetFolderAsync(folderData);

            // acquire file
            var _File = await _Folder.GetFilesAsync();
            Books books = new Books();

            for(int i = 0; i< _File.Count; i++)
            //foreach (StorageFile file in _File)
            {
                if (_File[i].Path.EndsWith(".xml"))
                {
                    books = GetBookDetail(folderData + "/" + _File[i].Name);
                    author[i] = books.Author;
                }
            }

            return author;
        }

        public static async Task<string[]> GetAllCategory(string folderData)
        {
            string[] category = null;
            // settings
            // same as (ms-appx:///MyFolder/MyFile.txt)
            var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            _Folder = await _Folder.GetFolderAsync(folderData);

            // acquire file
            var _File = await _Folder.GetFilesAsync();
            Books books = new Books();

            for (int i = 0; i < _File.Count; i++)
            //foreach (StorageFile file in _File)
            {
                if (_File[i].Path.EndsWith(".xml"))
                {
                    books = GetBookDetail(folderData + "/" + _File[i].Name);
                    category[i] = books.CategoryName;
                }
            }

            return category;
        }

        public static async Task<List<Books>> GetBookByAuthor(string authorName, string folderData)
        {
            List<Books> xmlBookByAuthor = new List<Books>();
            // settings
            // same as (ms-appx:///MyFolder/MyFile.txt)
            var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            _Folder = await _Folder.GetFolderAsync(folderData);

            // acquire file
            var _File = await _Folder.GetFilesAsync();
            Books books = new Books();

            foreach (StorageFile file in _File)
            {
                if (file.Path.EndsWith(".xml"))
                {
                    books = GetBookDetail(folderData + "/" + file.Name);
                    if (String.Equals(books.Name.Trim(), authorName.Trim(), StringComparison.OrdinalIgnoreCase))
                    {
                        xmlBookByAuthor.Add(books);
                    }
                }
            }

            return xmlBookByAuthor;
        }

        public static async Task<List<Books>> GetBookByCategory(string categoryName, string folderData)
        {
            List<Books> xmlBookByAuthor = new List<Books>();
            // settings
            // same as (ms-appx:///MyFolder/MyFile.txt)
            var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            _Folder = await _Folder.GetFolderAsync(folderData);

            // acquire file
            var _File = await _Folder.GetFilesAsync();
            Books books = new Books();

            foreach (StorageFile file in _File)
            {
                if (file.Path.EndsWith(".xml"))
                {
                    books = GetBookDetail(folderData + "/" + file.Name);
                    if (String.Equals(books.CategoryName.Trim(), categoryName.Trim(), StringComparison.OrdinalIgnoreCase))
                    {
                        xmlBookByAuthor.Add(books);
                    }
                }
            }

            return xmlBookByAuthor;
        }
    }
}
